﻿Imports MbUnit.Framework
Imports Subtext.TestLibrary
Imports FM.NetBouncer

<TestFixture()> _
Public Class NetBouncerFixture

    'Something dodgy about 

    <Test()> _
    Public Sub QuerystringSuccess()
        Using New HttpSimulator("/", "c:\inetpxub\").SimulateRequest(New Uri("http://ferruh.mavituna.com/?productId1=999"))
            Assert.IsTrue(NetBouncer.QueryStringAsInteger("productId1", RuleSets.IntegerSet).IsValid())
        End Using
    End Sub


    <Test()> _
    Public Sub QuerystringFail()
        Using New HttpSimulator("/", "c:\inetpuab\").SimulateRequest(New Uri("http://ferruh.mavituna.com/?productId2=asdsd"))
            Assert.IsFalse(NetBouncer.QueryStringAsInteger("productId2", RuleSets.IntegerSet).IsValid())
        End Using
    End Sub

    <Test()> _
    Public Sub QuerystringParamValue()
        Using New HttpSimulator("/", "c:\inetpub\").SimulateRequest(New Uri("http://ferruh.mavituna.com/?productId3=56456"))
            Dim ProductId As BaseParameter(Of String, Integer) = NetBouncer.QueryStringAsInteger("productId3", RuleSets.IntegerSet)

            Assert.IsTrue(ProductId.IsValid())

            If ProductId.IsValid() Then
                Assert.AreEqual(ProductId.Value, 56456)
            End If

        End Using
    End Sub


    <Test()> _
    Public Sub QuerystringAsString()
        Using New HttpSimulator("/", "c:\inetpub\").SimulateRequest(New Uri("http://ferruh.mavituna.com/?limit=1234567890113"))
            Dim LimitRule As New RuleSet(Of String)
            LimitRule.Rules.Add(New Rules.IsDataInRange(10, Integer.MaxValue))

            Dim ProductId As BaseParameter(Of String, String) = NetBouncer.QueryStringAsString("productId3", LimitRule)
            Assert.IsFalse(ProductId.IsValid())

            Assert.IsTrue((ProductId.Value Is Nothing OrElse ProductId.Value = String.Empty))
        End Using
    End Sub

    <RowTest()> _
    <Row("1", "", False)> _
    <Row("10", "", True)> _
    <Row("15", "", True)> _
    Public Sub FormAsString(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim PostColl As New Specialized.NameValueCollection()
        PostColl.Add("product1", input)

        Using New HttpSimulator("/", "c:\inetpub\").SimulateRequest(New Uri("http://ferruh.mavituna.com/"), PostColl)
            Dim LimitRule As New RuleSet(Of String)
            LimitRule.Rules.Add(New Rules.IsInRange(10, Integer.MaxValue))

            Dim ProductId As BaseParameter(Of String, String) = NetBouncer.FormAsString("product1", LimitRule)
            Assert.AreEqual(expectedType, ProductId.IsValid(), desc)

        End Using
    End Sub


    <RowTest()> _
    <Row("1", "", False)> _
    <Row("10", "", True)> _
    <Row("15", "", True)> _
    Public Sub FormAsInteger(ByVal input As String, ByVal desc As String, ByVal expectedType As Boolean)
        Dim PostColl As New Specialized.NameValueCollection()
        PostColl.Add("product1", input)

        Using New HttpSimulator("/", "c:\inetpub\").SimulateRequest(New Uri("http://ferruh.mavituna.com/"), PostColl)
            Dim LimitRule As New RuleSet(Of String)
            LimitRule.Rules.Add(New Rules.IsInRange(10, Integer.MaxValue))

            Dim ProductId As BaseParameter(Of String, Integer) = NetBouncer.FormAsInteger("product1", LimitRule)
            Assert.AreEqual(expectedType, ProductId.IsValid(), desc)

        End Using
    End Sub


    <Test()> _
    Public Sub InitTests()
        Dim NET As NetBouncer = NetBouncer.GetInstance()
    End Sub

End Class
